//
//  BDShopTableViewself.m
//  iOS_beboy
//
//  Created by wen on 2017/11/14.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDShopTableViewCell.h"
#import "YYStarView.h"
#import "BDShopModel.h"

@interface BDShopTableViewCell ()

@property (nonatomic, strong) YYAnimatedImageView *hongbaoImage;
@property (nonatomic, strong) YYStarView *starView;
@property (nonatomic, strong) UILabel *evaluationNumberLabel;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UILabel *rangLabel;
@property (nonatomic, strong) UILabel *tuiguangLabel;
@property (nonatomic, strong) UILabel *yishouLabel;

@property (nonatomic, strong) UIView *topLine;
@property (nonatomic, strong) UIView *bottomLine;

@end

@implementation BDShopTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.textLabel.font = [UIFont systemFontOfSize:18];
        
        _tuiguangLabel = [[UILabel alloc] init];
        _tuiguangLabel.backgroundColor = [UIColor colorWithHexString:@"#ff8400"];
        _tuiguangLabel.font = [UIFont systemFontOfSize:14];
        _tuiguangLabel.size = CGSizeMake(15, 15);
        _tuiguangLabel.text = @"推";
        _tuiguangLabel.textColor = [UIColor whiteColor];
        _tuiguangLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_tuiguangLabel];
        
        _starView = [[YYStarView alloc] init];
        [self.contentView addSubview:_starView];
        
        _evaluationNumberLabel = [[UILabel alloc]init];
        _evaluationNumberLabel.textColor = [UIColor colorWithHexString:@"#767676"];
        _evaluationNumberLabel.textAlignment = NSTextAlignmentRight;
        _evaluationNumberLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_evaluationNumberLabel];
        
        _addressLabel = [[UILabel alloc]init];
        _addressLabel.textColor = [UIColor colorWithHexString:@"#767676"];
        _addressLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_addressLabel];
        
        _rangLabel = [[UILabel alloc]init];
        _rangLabel.textColor = KBDColorBlue;
        _rangLabel.font = [UIFont systemFontOfSize:12];
        _rangLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_rangLabel];
        
        _yishouLabel = [[UILabel alloc] init];
        _yishouLabel.textColor = [UIColor colorWithHexString:@"#767676"];
        _yishouLabel.font = [UIFont systemFontOfSize:11];
        _yishouLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_yishouLabel];
        
        static YYImage *image =  nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            image = [YYImage imageNamed:@"hongbao_gif1.gif"];
        });
        _hongbaoImage = [[YYAnimatedImageView alloc] initWithImage:image];
        _hongbaoImage.size = CGSizeMake(18, 18);
        [self.contentView addSubview:_hongbaoImage];
        
        _topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 1)];
        _topLine.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
        [self addSubview:_topLine];
        
        _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 1)];
        _bottomLine.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
        [self addSubview:_bottomLine];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)configeWithModel:(id)model index:(NSInteger)index{
    BDShopModel *shopModel = model;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:shopModel.cover] placeholderImage:[UIImage imageNamed:@"shop_moren.jpg"]];
    if (index < 0) {
        self.textLabel.text = shopModel.title;
    }else{
        self.textLabel.text = [NSString stringWithFormat:@"%zd.%@", index + 1, shopModel.title];
    }
    
    [self.starView displayRating:shopModel.score];    //评分
    //        [self.starView displayRating:4];
    self.evaluationNumberLabel.text = [NSString stringWithFormat:@"%zd人评价", shopModel.commentCount];
    self.yishouLabel.text = [NSString stringWithFormat:@"已售%@",shopModel.sales];
    self.addressLabel.text = shopModel.address;                  //地址
    
    self.tuiguangLabel.hidden = YES;
    if (shopModel.priority == BDShopPriorityModeSigning) {
        self.textLabel.textColor = [UIColor colorWithHexString:@"#ff7e00"];
    }else if (shopModel.priority == BDShopPriorityModePromote) {
        self.textLabel.textColor = [UIColor colorWithHexString:@"#f54336"];
        self.tuiguangLabel.hidden = NO;
    }else{
        self.textLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    }
    
    if (shopModel.hongbao) {
        self.hongbaoImage.hidden = NO;
    }else{
        self.hongbaoImage.hidden = YES;
    }
    
    NSString *distansStr = nil;
    if (shopModel.distance < 0.5) {
        distansStr = @"<500m";
    }else if (shopModel.distance > 50) {
        distansStr = @">50 km";
    }else{
        distansStr = [NSString stringWithFormat:@"%.1f km",shopModel.distance];
    }
    self.rangLabel.text = distansStr;  //与用户的距离
    
    if (index == -2) {
        _topLine.hidden = YES;
        _bottomLine.hidden = YES;
    }else{
        if (index == 0) {
            _topLine.hidden = NO;
        }else{
            _topLine.hidden = YES;
        }
    }
}

- (void)layoutSubviews{
    self.imageView.size = CGSizeMake(64, 64);
    self.imageView.centerY = self.height / 2.0;
    self.imageView.right = self.width - 15;
    
    [self.textLabel sizeToFit];
    self.textLabel.top = 15;
    self.textLabel.left = 15;
    CGFloat titleMaxWidth = self.imageView.left - 10 - self.textLabel.left - (self.tuiguangLabel.hidden == YES ? 0 : 20) - (self.hongbaoImage.hidden == YES ? 0 : 20);
    self.textLabel.width = MIN(titleMaxWidth, self.textLabel.width);
    
    self.tuiguangLabel.centerY = self.textLabel.centerY;
    self.tuiguangLabel.left = self.textLabel.right + 5;
    
    self.hongbaoImage.centerY = self.tuiguangLabel.centerY;
    self.hongbaoImage.left = (self.tuiguangLabel.hidden ? self.textLabel.right : self.tuiguangLabel.right) + 5;
    
    [self.starView setStarWidth:12 starSpace:1.2 andDelegate:nil];
    self.starView.isIndicator =YES;
    self.starView.top = self.textLabel.bottom + 5;
    self.starView.left = self.textLabel.left;
    
    [self.evaluationNumberLabel sizeToFit];
    self.evaluationNumberLabel.left = self.starView.right + 8;
    self.evaluationNumberLabel.centerY = self.starView.centerY;
    
    [self.yishouLabel sizeToFit];
    self.yishouLabel.left = self.evaluationNumberLabel.right + 12;
    self.yishouLabel.centerY = self.evaluationNumberLabel.centerY;
    
    [_rangLabel sizeToFit];
    
    [_addressLabel sizeToFit];
    _addressLabel.left = self.textLabel.left;
    _addressLabel.top = _starView.bottom + 5;
    _addressLabel.width = MIN(_addressLabel.width, self.imageView.left - _addressLabel.left - 10 - _rangLabel.width - 10);
    
    _rangLabel.left = _addressLabel.right + 10;
    _rangLabel.centerY = _addressLabel.centerY;
    
    
    _topLine.size = CGSizeMake(self.width, 1);
    _topLine.top = 0;
    _topLine.left = 0;
    
    _bottomLine.size = CGSizeMake(self.width, 1);
    _bottomLine.bottom = self.height;
    _bottomLine.left = 0;
}

@end
